package edu.java.texbooks.scjp.threads.test14;

/**
 * 
 * This example is from page 724-725, SCJP book. It is related with wait() and
 * notify() methods
 * 
 * <p>
 * I have a QUESTION: What happens when execute notify() before wait() method.
 * </p>
 * 
 */
public class Reader extends Thread {

	// --- Instance Variables --------------------------------------------------
	Calculator calculator = null;

	// --- Constructors --------------------------------------------------------
	public Reader(String name, Calculator calculator) {
		super(name);
		this.calculator = calculator;
	}

	// --- Methods -------------------------------------------------------------
	@Override
	public void run() {

		synchronized (calculator) {
			System.out.println(this.getName() + " goes to sleep...");
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			System.out.println(this.getName() + " is waiting for result.");

			try {
				calculator.wait();
			} catch (InterruptedException ie) {
				System.out.println(this.getName() + " is interrupted.");
			}
			System.out.println(this.getName() + " prints result: "
					+ this.calculator.getTotal());
		}

	}
	
	

}
